<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class model_talks extends base_model{

	//hook mod_talks_start.php
	private $table;

	function __construct() {
		//hook mod_talks_init_init_before.php
		parent::__construct();
		$this->table = 'talks';
		//hook mod_talks_init_init_end.php
	}

	public function talks_add($fid = 0, $uid = 0,$title = '', $content = '',$aname='',&$ttid) {
		$ttid = $tid = $this->db->maxid($this->table.'-tid', '+1');
		//hook mod_talks_talks_add.php
		return $this->db->set( $this->table.'-tid-'.$tid, 
					array(	'tid'=>$tid,'fid'=>$fid,'uid'=>$uid,
							'title'=> $title ,'aname'=>$aname,'content'=>$content,
							'posttime'=> date('Y-m-d H:i:s')));
	}

	public function talks_edit($tid , $fid = 0, $title = '', $content = '') {
		$arr = array();
		if($fid >= 0)		$arr['fid'] = $fid;
		if($title != '')	$arr['title'] = $title;
		if($content != '')	$arr['content'] = $content;
		if(empty($arr)) return false;
		//hook mod_talks_talks_edit.php
		return $this->db->update( $this->table.'-tid-'.$tid, $arr);
	}
	
	public function talks_edit_tj($tid , $met = '+', &$lc) {
		$arr = $this->db->get($this->table.'-tid-'.$tid);
		if(empty($arr))
			return false;
		$lc = ($met == '+') ? $arr['ttj'] + 1 : $arr['ttj'];
		$tj = ($met == '+') ? $arr['tj'] + 1 : $arr['tj'] - 1;
		//hook mod_talks_talks_edit_tj.php
		return $this->db->update( $this->table.'-tid-'.$tid, 
					array('tj'=> $tj,'ttj'=>$lc));
	}

	public function talks_edit_allstick($tid) {
		$arr = $this->db->get($this->table.'-tid-'.$tid);
		if(empty($arr))
			return false;
		$arr['allstick'] = $arr['allstick']==0 ? 1:0;
		//hook mod_talks_talks_edit_allstick.php
		return $this->db->update( $this->table.'-tid-'.$tid, 
					array('allstick'=> $arr['allstick']));
	}

	public function talks_edit_stick($tid) {
		$arr = $this->db->get($this->table.'-tid-'.$tid);
		if(empty($arr))
			return false;
		$arr['isstick'] = $arr['isstick']==0 ? 1:0;
		//hook mod_talks_talks_edit_stick.php
		return $this->db->update( $this->table.'-tid-'.$tid, 
					array('isstick'=> $arr['isstick']));
	}

	public function talks_edit_digest($tid) {
		$arr = $this->db->get($this->table.'-tid-'.$tid);
		if(empty($arr))
			return false;
		$arr['isdigest'] = $arr['isdigest']==0 ? 1:0;
		//hook mod_talks_talks_edit_digest.php
		return $this->db->update( $this->table.'-tid-'.$tid, 
					array('isdigest'=> $arr['isdigest']));
	}

	public function talks_delete($tid ) {
		
		//hook mod_talks_talks_delete.php
		return $this->db->delete( $this->table.'-tid-'.$tid);
	}
	
	public function talks_list($fid = 0, $start = 0, $limit = 0 ,$digest = false) {
	//活跃
		$cond = array('fid'=>$fid, 'isstick'=>0, 'allstick'=>0);
		if($digest) $cond['isdigest'] = 1;
		//hook mod_talks_talks_before_list.php
		$arr = $this->db->index_fetch($this->table , 'tid'
										, $cond, array('time'=> -1)
										, $start, $limit);

		//hook mod_talks_talks_list.php
		return $arr;
	}
	
	public function talks_all_new_list( $start = 0, $limit = 0) {
		$cond = array('fid'=>array('!=' =>0 ),'allstick'=>0);
		return $this->db->index_fetch($this->table , 'tid'
										, $cond, array('tid'=> -1)
										, $start, $limit);
	}
	
	public function talks_of_allstick_list( $start = 0, $limit = 0, $fid = -1) {
		$cond = array('allstick'=>1);
		if($fid != 0) $cond['fid'] = array('!=' =>0);
		return $this->db->index_fetch($this->table , 'tid'
										, $cond, array('tid'=> -1)
										, $start, $limit);
	}
	
	public function talks_of_stick_list($fid, $start = 0, $limit = 0) {
		$cond = array('fid'=>$fid,'isstick'=>1, 'allstick'=>0);
		return $this->db->index_fetch($this->table , 'tid'
										, $cond, array('tid'=> -1)
										, $start, $limit);
	}
	
	public function talks_search_list($keywd = '', $start = 0, $limit = 0 ) {
		//hook mod_talks_talks_search_list.php
		return $this->db->index_fetch($this->table , 'tid'
										, array(
											'title'=>array('LIKE'=>"$keywd"),
											'fid'=>array('!=' =>0 ) )
										, array('tid'=> -1)
										, $start, $limit);
	}
	
	public function talks_search_count($keywd = '') {
		//hook mod_talks_talks_search_count.php
		return $this->db->index_count($this->table, array(
											'title'=>array('LIKE'=>"$keywd"),
											'fid'=>array('!=' =>0 ) ));
	}
	
	public function talks_of_users_list($uid, $start = 0, $limit = 0 ) {
		//hook mod_talks_talks_of_users_list.php
		return $this->db->index_fetch($this->table , 'tid'
										, array('uid'=>$uid), array('tid'=> -1)
										, $start, $limit);
	}
	
	public function talks_of_users_count($uid) {
		//hook mod_talks_talks_of_users_count.php
		return $this->db->index_count($this->table, array('uid'=>$uid));
	}
	
	public function talks_of_allstick_count($all = false,$fid = -1,$digest = false) {
		$cond = array('allstick'=>1);
		if(!$all) {
			$cond['fid'] = ($fid >= 0) ? $fid : array('!=' =>0);
		}
		if($digest) $cond['isdigest'] =1;
		//hook mod_talks_talks_of_allstick_count.php
		return $this->db->index_count($this->table, $cond);
	}
	
	public function talks_of_stick_count($fid,$digest = false) {
		$cond = array('fid'=>$fid,'isstick'=>1, 'allstick'=>0);
		if($digest) $cond['isdigest'] =1;
		//hook mod_talks_talks_of_stick_count.php
		return $this->db->index_count($this->table, $cond);
	}
	
	public function talks_of_digest_count($fid) {
		$cond = array('fid'=>$fid,'isdigest'=>1);
		//hook mod_talks_talks_of_digest_count.php
		return $this->db->index_count($this->table, $cond);
	}
	
	public function talks_of_gar_count() {
		$cond = array('fid'=>0);
		//hook mod_talks_talks_of_gar_count.php
		return $this->db->index_count($this->table, $cond);
	}
	
	public function talks_show($tid,$do_tj = false) {
		//hook mod_talks_talks_show.php
		$tid = max(0, intval($tid));
		$arr = $this->db->get($this->table.'-tid-'.$tid);
		if($do_tj){
			if(empty($arr))
				return $arr;
			$arr['rtj'] = $arr['rtj'] + 1;
			//hook mod_talks_talks_edit_rtj.php
			$this->db->update( $this->table.'-tid-'.$tid, 
					array('rtj'=> $arr['rtj'],'time'=> $arr['time']));
		}
		return $arr;
	}

	//hook mod_talks_end.php
	
}
/*{TranslateTagEnd}*/

?>